package com.gaoda.booksystem.web;

import com.gaoda.booksystem.entity.User;
import com.gaoda.booksystem.service.UserService;
import com.gaoda.booksystem.sysService.SysAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.security.Principal;
import java.util.Date;

/**
 * @author: LuckyBear
 * @date: 2018-12-12 9:30
 * @description:
 */
@Controller
public class loginController {


    @Autowired
    private UserService userService;

    @Autowired
    private SysAdminService sysAdminService;

    //登录
    @RequestMapping("/login")
    public String login() {
        return "login";
    }

    //注销登录
    @RequestMapping("/loginout")
    public String loginout() {
        return "loginout";
    }

    //根据权限跳转页面
    @RequestMapping("/qufen")
    public String qufen(Principal principal,HttpSession session) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        UserDetails userDetails = (UserDetails) auth.getPrincipal();
        String username = userDetails.getUsername();
        User user = userService.findUserByName(username);
        if (auth.getAuthorities().toString().equals("[user]")) {
            System.out.println("我是用户");
            return "redirect:index";//如果是客户登录
        } else {
            System.out.println("我是后台");
            session.setAttribute("admin",user);
            return "sys/home";//如果是后台管理人员登录
        }
    }

    //跳转注册页面
    @RequestMapping("/register")
    public String register() {
        return "register";
    }

    //注册
    @RequestMapping("/addUser")
    public String addUser(User user) {
        System.out.println("111");
        Date date = new Date();
        user.setUserTime(date);
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        user.setPassword(encoder.encode(user.getPassword().trim()));
        userService.insert(user);
        System.out.println(user);
        return "login";
    }

    //跳转修改用户信息页面
    @RequestMapping("/revampUser")
    public String revampUser(Principal principal, Model model) {
        String username = principal.getName();
        User user = userService.findUserByName(username);
        model.addAttribute("user", user);
        return "user/revampUser";
    }

    //修改用户信息
    @RequestMapping("/revamp")
    public String revamp(User user, Principal principal) {
        String username = principal.getName();
        user.setUsername(username);
        userService.updateUser(user);
        return "redirect:revampUser";
    }
    //跳转修改用户密码页面
    @RequestMapping("/revampUserPwd")
    public String revampUserPwd() {
        return "user/revampPassword";
    }
    //修改用户密码
    @RequestMapping("/revampPwd")
    public String revampPwd(User user, Principal principal) {
        String username = principal.getName();
        user.setUsername(username);
        System.out.println(user.getPassword());
        String pwd = new BCryptPasswordEncoder().encode(user.getPassword().trim());
        user.setPassword(pwd);
        userService.updatePwd(user);
        return "redirect:revampUser";
    }

    //跳转到修改密码页面
    @RequestMapping("/modifyAdminPwd")
    public String modifyAdminPwd(){
        return "/sys/modifyAdminPwd";
    }

    //提交密码进行修改
    @RequestMapping("/submitPwd")
    @ResponseBody
    public String sumbitPwd(User user,Principal principal){
        System.out.println("修改的信息"+user);
        //管理员自己修改密码 根据adminNo进行修改
        //修改前将新密码进行加密
        String username = principal.getName();
        user.setUsername(username);
        user.setPassword(new BCryptPasswordEncoder().encode(user.getPassword().trim()));
        sysAdminService.updatePwd(user);
        return "success";
    }
}
